const Database = require('better-sqlite3');
const path = require('path');

const dbPath = path.join(process.cwd(), 'data', 'delivery-management.db');

console.log('=== 数据库状态检查 ===');
console.log('数据库路径:', dbPath);

try {
  const db = new Database(dbPath);
  
  // 检查所有表
  console.log('\n📋 数据库中的所有表:');
  const tables = db.prepare(`
    SELECT name FROM sqlite_master WHERE type='table' ORDER BY name
  `).all();
  
  tables.forEach(table => {
    console.log(`  - ${table.name}`);
  });
  
  // 检查 parameter_field_metadata 表
  const parameterFieldMetadataExists = tables.some(t => t.name === 'parameter_field_metadata');
  console.log('\n🔍 parameter_field_metadata 表存在:', parameterFieldMetadataExists);
  
  if (parameterFieldMetadataExists) {
    // 检查字段元数据数量
    const fieldCount = db.prepare('SELECT COUNT(*) as count FROM parameter_field_metadata').get();
    console.log('字段元数据总数:', fieldCount.count);
    
    // 检查不同表名的数量
    const tableNames = db.prepare(`
      SELECT DISTINCT table_name, COUNT(*) as field_count 
      FROM parameter_field_metadata 
      GROUP BY table_name 
      ORDER BY table_name
    `).all();
    
    console.log('\n📊 各表的字段数量:');
    tableNames.forEach(table => {
      console.log(`  ${table.table_name}: ${table.field_count} 个字段`);
    });
    
    // 检查 table_c2 的字段
    const c2Fields = db.prepare(`
      SELECT field_name, data_type, data_source, is_required 
      FROM parameter_field_metadata 
      WHERE table_name = 'table_c2' 
      ORDER BY field_sequence
    `).all();
    
    console.log('\n🔍 table_c2 的字段详情:');
    if (c2Fields.length > 0) {
      c2Fields.forEach(field => {
        console.log(`  ${field.field_name}: ${field.data_type} | 来源: ${field.data_source} | 必填: ${field.is_required ? '是' : '否'}`);
      });
    } else {
      console.log('  ❌ table_c2 没有字段元数据');
    }
  } else {
    console.log('  ❌ parameter_field_metadata 表不存在');
    console.log('  💡 需要运行字段元数据初始化脚本');
  }
  
  // 检查设备类型映射表
  const deviceTypeMappingExists = tables.some(t => t.name === 'device_type_mappings');
  console.log('\n🔍 device_type_mappings 表存在:', deviceTypeMappingExists);
  
  if (deviceTypeMappingExists) {
    const mappingCount = db.prepare('SELECT COUNT(*) as count FROM device_type_mappings').get();
    console.log('设备类型映射数量:', mappingCount.count);
    
    // 检查一些映射示例
    const sampleMappings = db.prepare(`
      SELECT classification_code, naming_table, position_table, base_params_table 
      FROM device_type_mappings 
      LIMIT 5
    `).all();
    
    console.log('\n📋 设备类型映射示例:');
    sampleMappings.forEach(mapping => {
      console.log(`  ${mapping.classification_code}: 命名表=${mapping.naming_table}, 位置表=${mapping.position_table}, 基础表=${mapping.base_params_table}`);
    });
  }
  
  // 检查设备实例表
  const deviceInstanceTables = tables.filter(t => t.name.startsWith('device_instances_'));
  console.log('\n🔍 设备实例表:');
  if (deviceInstanceTables.length > 0) {
    deviceInstanceTables.forEach(table => {
      const count = db.prepare(`SELECT COUNT(*) as count FROM ${table.name}`).get();
      console.log(`  ${table.name}: ${count.count} 个实例`);
    });
  } else {
    console.log('  ❌ 没有找到设备实例表');
  }
  
  db.close();
  console.log('\n✅ 数据库状态检查完成');
  
} catch (error) {
  console.error('❌ 数据库检查失败:', error);
}
